package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.model.request.AddProductImgRequest;
import com.ytjj.qmyx.admin.model.request.ProductImgRequest;
import com.ytjj.qmyx.admin.service.MemberService;
import com.ytjj.qmyx.admin.service.ProductImgService;
import com.ytjj.qmyx.mall.model.ProductImg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品图片管理
 */
@RestController
@RequestMapping("/productImg")
public class ProductImgController {

    @Autowired
    private ProductImgService productImgService;

    /**
     * 根据条件查询商品图片
     * @return
     */
    @PostMapping(value = "/list")
    public CommonResult list(@RequestBody ProductImgRequest productImgRequest) {
        CommonPage<ProductImg> pageList = productImgService.list(productImgRequest);
        return CommonResult.success(pageList);
    }

    /**
     * 添加商品图片
     * @return
     */
    @PostMapping(value = "/create")
    @OperLog(operModule = "商品图片模块", operDesc = "添加商品图片", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult create(@RequestBody AddProductImgRequest addProductImgRequest) {
        int count = productImgService.create(addProductImgRequest);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改商品图片
     * @return
     */
    @PostMapping(value = "/update")
    @OperLog(operModule = "店家商品模块", operDesc = "修改商品图片", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult update(@RequestBody ProductImg productImg) {
        int count = productImgService.update(productImg);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 删除商品图片
     * @return
     */
    @PostMapping(value = "/delete")
    @OperLog(operModule = "店家商品模块", operDesc = "删除商品图片", operType = SysLogConstants.OPERATION_DELETE)
    public CommonResult delete(@RequestParam Integer id) {
        int count = productImgService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 一键批量删除商品图片
     * @return
     */
    @PostMapping(value = "/delProductImg")
    public CommonResult delProductImg(@RequestBody ProductImgRequest productImgRequest) {
        int count = productImgService.delProductImg(productImgRequest);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @PostMapping("/getAllImg")
    public CommonResult getAllImg(@RequestBody ProductImgRequest productImgRequest) {
        return CommonResult.success(productImgService.getAllImg(productImgRequest.getProductId(), productImgRequest.getType()));
    }

}
